package com.test.application.http.jdk;

import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class HttpPostExample {
    public static void main(String[] args) throws Exception {
        String url = "http://localhost:8241/sdk-demo/v241/tianyiyun/testPost";
        URL obj = new URL(url);
        HttpURLConnection con = (HttpURLConnection) obj.openConnection();

        // 设置请求方法和参数类型
        con.setRequestMethod("POST");
        con.setDoOutput(true);
        con.setRequestProperty("Content-Type", "application/json");

        // 构建 JSON 数据
        String jsonInputString = "{\"aliasName\":\"测试\"}";

        try (OutputStream os = con.getOutputStream()) {
            byte[] input = jsonInputString.getBytes("utf-8");
            os.write(input, 0, input.length);
        }

        // 获取响应
        int responseCode = con.getResponseCode();
        System.out.println("Response Code : " + responseCode);
    }
}